#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2018/5/30 15:41
# @Author  : simple0426
# @Email   : istyle.simple@gmail.com
# @File    : md5sum.py
# @Software: PyCharm
# @desc    : 检测文件的md5,，同shell下的md5sum

import sys, hashlib, os

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
if len(sys.argv) != 2:
    exit('Usage:%s [filename]' % sys.argv[0])
filename = sys.argv[1]
filepath = os.path.join(BASE_DIR, filename)
filesize = os.path.getsize(filepath)

md5_obj = hashlib.md5()
readsize = 0
with open(filepath, 'rb') as f:
    while readsize < filesize:
        data = f.read(1024)
        md5_obj.update(data)
        readsize += len(data)
print(md5_obj.hexdigest(), filename)
